<include file="Public/header" /> 

<!--主窗体-->
<div class="container app">
	<!--左侧菜单-->
	<include file="Public/sidebar" /> 
	<!--左侧菜单//-->
	
	<!-- mainCon -->
	<div id="mainCon" class="mainCon">
		<h4>添加商品</h4>
		<form class="form-horizontal" id="myform">
			
			<h5>基本信息</h5>
		
			<div class="form-group">
				<label for="cat_id" class="col-sm-2 control-label">分类</label>
				<div class="col-sm-5">
					<select name="cat_id" class="form-control">
						<option value="0">选择分类</option>
						<foreach name="cate" item="vo">
							<option value="<{$vo['id']}>"><{$vo['name']}></option>
							<if condition="!empty($vo['sub'])">
								<foreach name="vo['sub']" item="svo" key="sk">
									<option value="<{$svo['id']}>">
									├──
									<{$svo['name']}></option>		
								</foreach>
							</if>
						</foreach>
					</select>
				</div>
			</div>
		
			<div class="form-group">
				<label for="name" class="col-sm-2 control-label">商品名称</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="name" id="name" value="" placeholder="商品名称">
				</div>
			</div>
			
			<div class="form-group">
				<label for="goods_no" class="col-sm-2 control-label">商品编号</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="goods_no" id="goods_no" value="" placeholder="商品编号">
				</div>
			</div>
			
			
			<div class="form-group">
				<label for="sort" class="col-sm-2 control-label">排序</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="sort" id="sort" value="100" placeholder="自定义排序，值越大越靠前.">
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-2 control-label">上传图片</label>
				<div class="radio col-sm-5">
					<a class="btn btn-default" id="uploadify">上传图片</a> <span style="margin-top:10px;float:left;color:#999">（仅允许上传1MB以内的图片）</span>
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-2 control-label"></label>
				<div id="showimg">
					
				</div>
				<input type="hidden" id="upimg" name="upimg" value=""/>
			</div>
			
			<h5>商品规格</h5>
			<div class="form-group">
				<label for="pro_no" class="col-sm-2 control-label">货号</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="pro_no" id="pro_no" value="" placeholder="商品货号">
				</div>
			</div>
			
			<div class="form-group">
				<label for="store_nums" class="col-sm-2 control-label">库存</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="store_nums" id="store_nums" value="" placeholder="商品库存">
				</div>
			</div>
			
			<!-- <div class="form-group">
				<label for="warning_line" class="col-sm-2 control-label">预警线</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="warning_line" id="warning_line" value="" placeholder="库存预警线">
				</div>
			</div> -->
			
			<div class="form-group">
				<label for="sell_price" class="col-sm-2 control-label">零售价</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="sell_price" id="sell_price" value="" placeholder="零售价">
				</div>
			</div>
			
			<div class="form-group">
				<label for="market_price" class="col-sm-2 control-label">市场价</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="market_price" id="market_price" value="" placeholder="市场价">
				</div>
			</div>
			
			<div class="form-group">
				<label for="cost_price" class="col-sm-2 control-label">成本价</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="cost_price" id="cost_price" value="0" placeholder="成本价">
				</div>
			</div>
			
			<div class="form-group">
				<label for="cost_price" class="col-sm-2 control-label">是否包邮</label>
				<div class="col-sm-5">
					<div class="bk10"></div>
					<label for="free_no">
						<input type="radio" name="free_delivery" value="0" id="free_no" checked/> 否
					</label>
					<label for="free_yes" style="margin-left:10px;">
						<input type="radio" name="free_delivery" value="1" id="free_yes"/> 是
					</label>
				</div>
			</div>
			
			<div class="form-group show_express_box" style="display:none">
				<label for="cost_price" class="col-sm-2 control-label">运费</label>
				<div class="col-sm-5">
					<div class="input-group">
						<div class="input-group-addon" style="width:55px;">快递 </div>
						<input type="text" class="form-control"  name="express" placeholder="快递费用" style="width:120px;">
					</div>
					<div class="bk5"></div>
					<div class="input-group">
						<div class="input-group-addon" style="width:55px;">平邮 </div>
						<input type="text" class="form-control" name="snail" placeholder="平邮费用" style="width:120px;">
					</div>
					<div class="bk5"></div>
					<div class="input-group">
						<div class="input-group-addon" style="width:55px;">EMS</div>
						<input type="text" class="form-control" name="ems" placeholder="EMS费用" style="width:120px;">
					</div>
				</div>
			</div>
			
			<div class="form-group">
				<label for="cost_price" class="col-sm-2 control-label">规格</label>
				<div class="col-sm-5">
					<div class="bk5"></div>
					<label for="open_attr">
						<input type="radio" name="g_attr" value="1" id="open_attr"/> 开启规格
					</label>
					<label for="close_attr" style="margin-left:10px;">
						<input type="radio" name="g_attr" value="0" id="close_attr" checked/> 关闭规格
					</label>
					<div id="goodsAttr" style="display:none">
						<table>
							<tr>
								<td>
									<select class="attr_one">
										<option value=""></option>
										<option value=""></option>
									</select>
								</td>
								<td>
									<a class="btn btn-default">添加属性</a>
								</td>
							</tr>
						</table>
					</div>
				</div>
			</div>
			
			<div class="form-group">
				<label for="cost_price" class="col-sm-2 control-label">商品详情</label>
				<div class="col-sm-5">
					<textarea name="content" id="content" style="width:680px;height:480px;font-size:16px;"></textarea>
					<{:A('Editor')->buildEditor('content');}>
				</div>
			</div>
			
			<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="submit" class="btn btn-default col-sm-2">保存</button>
				</div>
			</div>
		</form>
		
	</div>
	<!-- mainCon// -->
	
</div>
<!--主窗体//-->

<script src="__PUBLIC__/js/layer/layer.js"></script>
<script src="__PUBLIC__/js/validate/jquery.validate.min.js"></script>
<script src="__PUBLIC__/js/validate/messages_zh.js"></script>
<script src="__PUBLIC__/js/uploadify/jquery.uploadify.min.js"></script>
<script>

<if condition="empty($cate)">
layer.alert("现在还没有商品分类，请先添加商品分类。",{icon: 1},function(){
	window.location.href = "<{:U('Admin/Shopping/addCate',array('k'=>4,'sk'=>3))}>";
});
</if>

// 判断英文字符 
jQuery.validator.addMethod("isEnglish", function(value, element) {       
     return this.optional(element) || /^[A-Za-z0-9]+$/.test(value);       
}, "只能包含英文字符。"); 

$(function(){
	
	//是否免邮
	$("#free_no,#free_yes").click(function(){
		var checked_val = $('input[name="free_delivery"]:checked').val();
		if (checked_val == '1') {
			$(".show_express_box").show();
		} else {
			$(".show_express_box").hide();
		}
	});

	
//	$('input[name="free_delivery"]').
	
	$("#myform").validate({
		rules: {
			name : {
				required : true
            },
            alias : {
            	required : true,
            	isEnglish : true
            }
		},
		messages: {
			
		},
		submitHandler : function(form){
			var ajaxUrl = "<{:U('Admin/Shopping/do_addCate')}>";
			 $.post(ajaxUrl,$(form).serialize(),function(r){
    			if (r.error == 1){
    				layer.alert(r.errmsg,{icon: 2});
    			}else{
    				layer.alert(r.errmsg,{icon: 1},function(){
    					window.location.href = "<{:U('Admin/Shopping/cateList',array('k'=>4,'sk'=>3))}>";
    				});
    			}
    			return false;
    		});
        	return false
		}
	});
	
	// 文件上传
	$("#uploadify").uploadify({
	//	'formData': {"uptype":"cate"},
		'swf'           : '__PUBLIC__/js/uploadify/uploadify.swf',
		'uploader'      : "<{:U('Admin/Gallery/uploadImg')}>",
		'fileTypeExts'	: '*.jpg;*.png;*.gif',
		'width'			: 90,
		'buttonText' : '<a class="btn btn-default" id="uploadify">上传图片</a>',
		'hideButton' : true,
		'multi': false,
		'fileObjName' : 'imgFile',
		'fileSizeLimit' : '1MB',
		'onUploadError' : function(back) {
			
		},
		'onUploadStart' : function(file) {

    	},
		'onUploadSuccess' : function(file,data,response) {
			var r = jQuery.parseJSON(data);
			if(r.error == 0){
				var htm = '<div style="width:100px;height:100px;border:1px solid #ccc;overflow:hidden;float:left;">';
				htm += '<img src="/'+r.imgUrl+'" style="width:100px;height:100px;float:left;margin:0 auto;padding:5px;">';
				htm += '<div onclick="removeImg();" style="height:30px;width:100%;background:#000;opacity:.6;text-align:center;z-index:3;position:relative;bottom:30px;color:#fff;line-height:30px;float:left;cursor:pointer;">删除</div>';
				htm += '</div>';
				$("#showimg").append(htm);
				$("#upimg").val(r.imgUrl);
			}
		},
	
	});
	
});

function removeImg(){
	$("#showimg").html('');
	$("#upimg").val('');
}
</script>
<style>
.uploadify-queue{
	display:none;
}
h5{
	height:35px;
	line-height:35px;
	padding-left:15px;
	background:#f0f0f0;
}
</style>
<include file="Public/footer" /> 